2
2
.
.
4
4
.
.
1
1
0
0
R
R
e
e
t
t
u
u
r
r
n
n
-
-
V
V
i
i
e
e
w
w
-
-
J
J
S
S
P
P
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
This tutorial shows how to return JSP from the Controller.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @RequestMapping. Includes Tomcat HTTP Server.
index.jsp
http://localhost:8080/Hello
Tomcat
MyController
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: controller_returns_jsp (add Spring Boot Starters from the table)
Edit FIle: pom.xml (manually add jasper dependency to be able to compile JSP files)
Edit FIle: application.properties (specify location of JSP files)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside controllers package)
Create Directories: webapp/WEB-INF/jsp (inside src/main/ directory)
Create JSP file: index.jsp (inside jsp directory)
pom.xml (manually add jasper dependency)
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
application.properties (Directory: src/main/resources)
# JSP
spring.mvc.view.prefix = /WEB-INF/jsp/
spring.mvc.view.suffix = .jsp
MyController.java (Directory: src/main/java/.../controllers)
package com.ivoronline.controller_returns_jsp.controllers;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
@Controller
public class MyController {
@RequestMapping("/Hello")
public String hello(Model model) {
model.addAttribute("message", "Hello from Controller");
return "index";
}
}
index.jsp (Directory: src/main/webapp/WEB-INF/jsp)
<title>Hello from JSP</title>
${message}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello
Application Structure
pom.xml (manually add jasper dependency)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>